package com.fs.web;

import com.alibaba.fastjson.JSONObject;
import com.fs.pojo.Questions;
import com.fs.service.QuestionsService;
import com.fs.utils.BeanFactories;
import com.fs.utils.JsonData;
import com.fs.utils.ResponseJSONUtil;
import com.fs.vo.QuestionVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/question/*")
public class QuestionServlet extends HttpServlet {

    private QuestionsService questionsService = BeanFactories.getBean(QuestionsService.class);
    private HttpServletRequest req;
    private HttpServletResponse resp;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        this.req = request;
        this.resp = response;

        switch (req.getPathInfo()) {
            case "/getAllQuestionByPaperIdnClassPaperId":  getAllQuestionByPaperIdnClassPaperId() ; break;
            case "/getAllQuestionsByPaperId": getAllQuestionsByPaperId() ; break;
        }

    }



    private void getAllQuestionByPaperIdnClassPaperId() throws IOException {
        String paperId = req.getParameter("paperId");
        String classPaperId = req.getParameter("classPaperId");

        List<QuestionVO> vos = questionsService.queryAllQuestionsByPaperIdnClassPaperId(paperId,classPaperId);

        ResponseJSONUtil.responseJSON(JsonData.buildSuccess(vos),resp);
    }

    private void getAllQuestionsByPaperId() throws IOException {
        String paperId = req.getParameter("paperId");
        List<Questions> allQuestionsByQuestionId = questionsService.getAllQuestionsByPaperId(paperId);

        JsonData data = JsonData.buildSuccess(allQuestionsByQuestionId);

        resp.getWriter().println(JSONObject.toJSON(data));

    }
}
